﻿var site_domain = 'https://weapp.711688.net.cn/';       //主域名
var api_domain = 'https://weapp.711688.net.cn/';       //api域名
var userid = getCookie("userid");
//var token = getCookie("token");

var PageUrl = new pageUrl();
function pageUrl() {
    //会员
    this.User_Login = "/admin_wechat/pages/user/login.html"; //登录
    //订单
    this.Order_List = "/admin_wechat/pages/order/list.html"; //列表
    this.Order_Detail = "/admin_wechat/pages/order/detail.html"; //详情
    this.Order_Update_Discount = "/admin_wechat/pages/order/update_discount.html"; //修改折扣
    this.Order_Update_Shipfee = "/admin_wechat/pages/order/update_shipfee.html"; //修改配送费用
    this.Order_Send = "/admin_wechat/pages/order/send.html"; //发货
    this.Order_Action_List = "/admin_wechat/pages/order/action_list.html"; //操作列表
    this.Order_Manage = "/admin_wechat/pages/order/manage.html"; //订单操作
    //预约
    this.Appoint_Detail = "/admin_wechat/pages/appoint/detail.html"; //预约详情
}

function $_2(id) {
    return document.getElementById(id);
}
/*URL参数*/
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = decodeURI(window.location.search).substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return "";
}

//字符串是否空对象或者空值
function IsNullOrEmpty(str) {
    if (str == null || str == '')
        return true;
    return false;
}

function islogin() {
    token = getCookie("token");
    userid = getCookie("userid");
    if (userid == undefined || userid == "" || userid == null)
        return false;
    return true;
}

// 登录状态验证
function toLogin() {
    if (!islogin()) {
        var backurl = encodeURIComponent(document.location.href);
        toUrl(PageUrl.User_Login + "?backurl=" + backurl);
        return false;
    }
    return true;
}

//检查uid是否是登录ID
function checkUserId(uid) {
    var username = getQueryString("username");
    if (username != '') {
        if (username != getCookie("name")) {
            var backurl = encodeURIComponent(document.location.href);
            toUrl(PageUrl.User_Login + "?backurl=" + backurl);
            return false;
        }
    }
    //if (uid != userid) {
    //    var backurl = encodeURIComponent(document.location.href);
    //    toUrl(PageUrl.User_Login + "?backurl=" + backurl);
    //    return false;
    //}
    return true;
}

//是否是APP终端
function IsMobile() {
    var ua = navigator.userAgent.toLocaleLowerCase();
    if (ua.indexOf("wjb") > -1) {
        if (ua.indexOf("android") > -1) return 1;
        if (ua.indexOf("iphone") > -1) return 2;
    }
    return 0;
}

//跳转页面
function toUrl(url) {
    window.location.href = url;
}

//删除左右两端的空格
function trim(str) {
    if (str == null) return "";
    return str.replace(/(^\s*)|(\s*$)/g, "");
}


var scroll_state = true;
//滚动分页
function loadScroll(callback) {
    $(window).bind("scroll", function () {
        if (!scroll_state) return;  //停止滚动

        //当滚动条滚动时
        var scollTop = $(this).scrollTop() + $(this).height();
        var scollH = $(document).height();
        if (scollH - scollTop < 100) {
            if (wait.isShow) { return; } //如果正在加载，则返回  
            page++;
            callback(page);
        }
        return false;
    });
}

//上滑动时，检测是否是最后一条
function checkPage(data, page, msg, id) {
    msg = msg == "" ? data.msg : msg;    
    if (data.code == JsonCode.NoData || data.item == null) {
        pagecount = "1";
        if (id == null)
            Alert(msg);
        else {
            $("#" + id).css("display","");
            $("#" + id).html(msg);
        }
        return false;
    }
    
    if (data.code != JsonCode.success) {
        Alert(msg);
        return false;
    }
    
    if (data.item.pageCount < page && page > 1) {
        if (pagecount == "0") {
            if (id == null)
                Alert('到底了');
            else {
                $("#" + id).css("display", "");
                $("#" + id).html("到底了");
            }
        }
        pagecount = "1";
        return false;
    }
    return true;
}

var JsonCode = { SuccessCode: 0 }

//校验返回码,正常返回true,异常则输出message
function isSuccess(data) {
    if (data.Code == JsonCode.SuccessCode)
        return true;
    var msg = data.Message == "" ? "返回失败" : data.Message;
    Alert(msg);
    return false;
}


//显示消息
function Alert(msg) {
    toast.show(msg);
}

//设置cookies
function SetCookie(name, value, Days) {
    if (Days == null) Days = 30; //设置Cookie有效时间
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
}

//获取cookies
function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]); return null;
}

//删除cookies
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) {
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }
}


//网址验证
function IsURL(url) {
    var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
         + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@  
         + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184  
         + "|" // 允许IP和DOMAIN（域名） 
         + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.  
         + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名  
        + "[a-z]{2,6})" // first level domain- .com or .museum  
        + "(:[0-9]{1,4})?" // 端口- :80  
        + "((/?)|" // a slash isn't required if there is no file name  
        + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
    var re = new RegExp(strRegex);
    if (re.test(url)) {
        return (true);
    } else {
        return (false);
    }
}
//手机号验证
function isMobilePhone(str) {
    var reg = new RegExp("[0-9]{11}");
    if (reg.test(str))
        return true;
    return false;
}

//网址验证
function CheckUrl(str) {
    if (str.indexOf("http") > -1 || str.indexOf(".com") > -1 || str.indexOf(".cn") > -1 || str.indexOf(".cc") > -1 || str.indexOf(".con") > -1)
        return true;
    return false;
}